package ru.ifmo.itmokis.domain;

import java.io.Serializable;
import java.util.Map;
import javax.persistence.AttributeOverride;
import javax.persistence.CollectionTable;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.MapKeyJoinColumn;
import javax.persistence.Table;

/**
 *
 * @author Ivan Stepuk
 */
@Entity
@Table(name = "Products")
public class Product implements Serializable {

    private int id;
    private String name;
    private Time productionDuration;
    private Map<Material, Integer> recipe;

    @Id
    @GeneratedValue
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Embedded
    @AttributeOverride(name = "value", column =
    @Column(name = "productionDuration"))
    public Time getProductionDuration() {
        return productionDuration;
    }

    public void setProductionDuration(Time productionDuration) {
        this.productionDuration = productionDuration;
    }

    @ElementCollection
    @CollectionTable(name = "Recipes", joinColumns =
    @JoinColumn(name = "product"))
    @MapKeyJoinColumn(name = "material")
    @Column(name = "qty")
    public Map<Material, Integer> getRecipe() {
        return recipe;
    }

    public void setRecipe(Map<Material, Integer> recipe) {
        this.recipe = recipe;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Product other = (Product) obj;
        if (this.id != other.id) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 71 * hash + this.id;
        return hash;
    }

    @Override
    public String toString() {
        return name;
    }
}
